package com.ysd.lis.service.qi.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.qi.QiIndexItem;
import com.ysd.lis.entity.qi.QiIndexMain;
import com.ysd.lis.entity.qi.QiIndexMajor;
import com.ysd.lis.mapper.qi.QiIndexItemMapper;
import com.ysd.lis.mapper.qi.QiIndexMainMapper;
import com.ysd.lis.mapper.qi.QiIndexMajorMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.qi.QiIndexMainService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 指标维护 主数据
 * </p>
 *
 * @author LiuQi
 * @since 2024-01-29
 */
@Service
public class QiIndexMainServiceImpl extends ServiceImpl<QiIndexMainMapper, QiIndexMain> implements QiIndexMainService {

    @Autowired
    QiIndexMainMapper qiIndexMainMapper;

    @Autowired
    QiIndexItemMapper qiIndexItemMapper;

    @Autowired
    QiIndexMajorMapper qiIndexMajorMapper;

    @Override
    public Result getQiIndexMainList() {
        LambdaQueryWrapper<QiIndexMain> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QiIndexMain::getDelFlag, 0)
                .orderByAsc(QiIndexMain::getSeq);
        List<QiIndexMain> list = qiIndexMainMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", list);
    }

    @Override
    public Result addQiIndexMain(QiIndexMain qiIndexMain) {
        boolean save = this.save(qiIndexMain);
        if (save) {
            return Result.succ(1, "添加成功", null);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result updateQiIndexMain(QiIndexMain qiIndexMain) {
        boolean save = this.updateById(qiIndexMain);
        if (save) {
            return Result.succ(1, "编辑成功", null);
        } else {
            return Result.fail("编辑失败");
        }
    }

    @Override
    public Result deleteQiIndexMain(QiIndexMain qiIndexMain) {
        LambdaQueryWrapper<QiIndexItem>  lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QiIndexItem::getDelFlag, 0).eq(QiIndexItem::getIndexId, qiIndexMain.getId());
        Integer i = qiIndexItemMapper.selectCount(lambdaQueryWrapper);
        Assert.isTrue(i == 0, "此指标下有项目信息，禁止删除！");
        LambdaQueryWrapper<QiIndexMajor>  lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(QiIndexMajor::getDelFlag, 0).eq(QiIndexMajor::getIndexId, qiIndexMain.getId());
        Integer ii = qiIndexMajorMapper.selectCount(lambdaQueryWrapper1);
        Assert.isTrue(ii == 0, "此指标下有专业信息，禁止删除！");
        boolean delete = this.removeById(qiIndexMain.getId());
        if (delete) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败");
        }
    }

    @Override
    public Result findQiIndexMainAndTypeList(CommonPageDto pageParam) {
        LambdaQueryWrapper<QiIndexMain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(QiIndexMain::getDelFlag, 0);
        List<QiIndexMain> mainList = qiIndexMainMapper.selectList(queryWrapper);
        List<Map> iList = new ArrayList<>();
        for(QiIndexMain qiIndexMain : mainList){
            Map<String,Object> map = new HashMap<>();
            map.put("id",qiIndexMain.getId());
            map.put("indexName",qiIndexMain.getIndexName());
            map.put("indexCode",qiIndexMain.getIndexCode());
            map.put("indexType",qiIndexMain.getIndexType());
            map.put("isSearch","1");
            iList.add(map);
        }

        List<String> indexTypeList = mainList.stream().filter(ins-> ToolsUtils.isNotEmpty(ins.getIndexType()) ).map(QiIndexMain::getIndexType).distinct().collect(Collectors.toList());
        List<Map>  returnList = new ArrayList<>();

        for(int i = 0;i<indexTypeList.size() ;i++){
            String className = indexTypeList.get(i);
            Map<String,Object> map = new HashMap();
            map.put("id",i+1);
            map.put("indexCode",className);
            //yqdllist.stream().filter(a->a.get("code").equals(className)).findFirst().get().get("name")
            String indexName = "";
            if(className.equals("1")){
                indexName = "检验全过程质量指标";
            }else if(className.equals("2")){
                indexName ="新增检验全过程质量指标";
            }
            map.put("indexName",indexName);
            map.put("children",iList.stream().filter(is -> ToolsUtils.isNotEmpty(is.get("indexType"))&&is.get("indexType").equals(className)).collect(Collectors.toList()));
            map.put("isSearch","0");
            returnList.add(map);
        }
        return Result.succ(1, "查询成功", returnList);

    }
}
